/**
 * Created by 121 on 2016/4/08.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
    'calendar',
    'highCharts',
    'datepicker'
], function ($, common, ready, template, Collection, Model, dialog, calendar, highCharts, datepicker) {
    var index = function () {
        this.init();
    };
    $('.sidebar').css({'display': 'none'});
    $('header').css({'display': 'none'});
    $('.main').css({'margin': '50px 50px'});
    index.prototype = {
        init: function () {
            this.event();
            //this.fetch();
        },
        event: function () {
            var _this = this;
            var success = function () {
                if (start_time > end_time) {
                    Alert({msg: '开始与结束时间范围错误', type: 'error'});
                    return false;
                }

                $.ajax({
                    url: _global.url.api + 'data_report_info_detail',
                    type: 'get',
                    data: {ids: ids, type: type, start_time: start_time, end_time: end_time},
                    success: function (res) {
                        if (res.code != 0) {
                            Alert({msg: '此时间范围,没有数据', type: 'error'});
                            return false;
                        }
                        //if (report != 1) {
                        $('#jd-order-data-success').highcharts({
                            chart: {
                                zoomType: 'xy'
                            },
                            title: {
                                text: ''
                            },
                            xAxis: [{
                                tickInterval: res.data.tickInterval,
                                showFirstLabel: res.data.showFirstLabel,
                                categories: res.data.tab,
                                title: {
                                    text: res.data.title_x,
                                    style: {
                                        color: '#89A54E'
                                    }
                                },
                                labels: {
                                    rotation: -45,
                                    align: 'right',
                                    style: {
                                        fontSize: '10px'
                                    }
                                }
                            }],
                            yAxis: [{ // Primary yAxis
                                allowDecimals: false,
                                floor: 0,
                                labels: {
                                    format: '{value}',
                                    style: {
                                        color: '#89A54E'
                                    }
                                },
                                title: {
                                    text: '',
                                    style: {
                                        color: '#89A54E'
                                    }
                                }
                            }],
                            tooltip: {
                                shared: true
                            },
                            legend: {
                                layout: 'vertical',
                                align: 'right',
                                verticalAlign: 'middle',
                                backgroundColor: '#FFFFFF'
                            },
                            series: res.data.series

                        });
                        //} else {
                        $("#title_tab").html(template('data-item-tpl', {sale: res.data.series}));
                        console.log(res.data.series);
                        $("#data-list").html(template('data-item_tpl', {tab: res.data.tab, sales: res.data.series}));


                        //$('#sell-view').append(template('sell-view-tpl', {'data': res.data}))
                    }

                });

            }


            /*var fail = function (status) {
             $.ajax({
             url: _global.url.api + 'jd_goods/order/fail',
             type: 'get',
             data: {status: status},
             success: function (res) {
             console.log(res.data.tickInterval);
             $('#jd-order-data-fail').highcharts({
             chart: {
             zoomType: 'xy'
             },
             title: {
             text: ''
             },
             xAxis: [{
             tickInterval: res.data.tickInterval,
             categories: res.data.created_date
             }],
             yAxis: [{ // Primary yAxis
             labels: {
             format: '{value}单',
             style: {
             color: '#89A54E'
             }
             },
             title: {
             text: '成交笔数',
             style: {
             color: '#89A54E'
             }
             }
             }, { // Secondary yAxis
             title: {
             text: '销售额',
             style: {
             color: '#4572A7'
             }
             },
             labels: {
             format: '{value}元',
             style: {
             color: '#4572A7'
             }
             },
             opposite: true
             }],
             tooltip: {
             shared: true
             },
             legend: {
             layout: 'vertical',
             align: 'right',
             verticalAlign: 'middle',
             backgroundColor: '#FFFFFF'
             },
             series: [{
             name: '销售额',
             color: '#4572A7',
             type: 'spline',
             yAxis: 1,
             data: res.data.order_amount,
             tooltip: {
             valueSuffix: ' 元'
             }

             }, {
             name: '成交笔数',
             color: '#89A54E',
             type: 'spline',
             data: res.data.order_num,
             tooltip: {
             valueSuffix: '单'
             }
             }]
             });
             }
             });
             }
             var fail_reasons = function (status) {
             $.ajax({
             url: _global.url.api + 'jd_goods/order/fail/reasons',
             type: 'get',
             data: {status: status},
             success: function (res) {
             $('#jd-order-fail-reasons').highcharts({
             chart: {
             zoomType: 'xy'
             },
             title: {
             text: ''
             },
             xAxis: {
             categories: res.data.reasons,
             labels: {
             style: {
             fontSize: '20px'
             }
             }
             },
             yAxis: {
             labels: {
             format: '{value}单',
             style: {
             color: '#89A54E'
             }
             },
             title: {
             text: '成交笔数',
             style: {
             color: '#89A54E'
             }
             }
             },
             tooltip: {
             shared: true
             },
             legend: {
             layout: 'vertical',
             align: 'right',
             verticalAlign: 'middle',
             backgroundColor: '#FFFFFF'
             },
             series: [{
             name: '成交笔数',
             color: '#89A54E',
             type: 'column',
             data: res.data.num,
             tooltip: {
             valueSuffix: '单'
             }
             }]
             });
             }
             });
             }*/

            success();
            /*    fail();
             fail_reasons();*/
            $('#btn-success-status').find('a').on('click', function (e) {
                e.preventDefault();
                e.preventDefault();
                var status = $(e.target).attr('rel');
                success(status);
            });
            $('#btn-fail-status').find('a').on('click', function (e) {
                e.preventDefault();
                e.preventDefault();
                var status = $(e.target).attr('rel');
                fail(status);
            });
            $('#btn-fail-reasons-status').find('a').on('click', function (e) {
                e.preventDefault();
                e.preventDefault();
                var status = $(e.target).attr('rel');
                fail_reasons(status);
            });
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd'
                });
            });

            //查询
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                var start_time = $('#start_time').val();
                var end_time = $('#end_time').val();
                location.href = '/admin/see_data/' + ids + '/' + type + '/' + start_time + '/' + end_time + '/' + 0;
            })
        }
    };
    new index();
});